package com.iot08.common.utils;

/**
 * 多矩形判断  经纬度 是否是在中国区域
 * IsInsideChina
 * https://blog.csdn.net/rongge2008/article/details/50549940
 *
 * @Author: Weichao
 * @Date: 2024/08/13/16:48
 * @Description:
 */
public class Rectangle {
    public double West;
    public double North;
    public double East;
    public double South;
    public Rectangle(double latitude1, double longitude1, double latitude2, double longitude2)
    {
        this.West = Math.min(longitude1, longitude2);
        this.North = Math.max(latitude1, latitude2);
        this.East = Math.max(longitude1, longitude2);
        this.South = Math.min(latitude1, latitude2);
    }

    private static Rectangle[] region = new Rectangle[]
            {
                    new Rectangle(49.220400, 079.446200, 42.889900, 096.330000),
                    new Rectangle(54.141500, 109.687200, 39.374200, 135.000200),
                    new Rectangle(42.889900, 073.124600, 29.529700, 124.143255),
                    new Rectangle(29.529700, 082.968400, 26.718600, 097.035200),
                    new Rectangle(29.529700, 097.025300, 20.414096, 124.367395),
                    new Rectangle(20.414096, 107.975793, 17.871542, 111.744104),

            };
    private static Rectangle[] exclude = new Rectangle[]
            {
                    new Rectangle(25.398623, 119.921265, 21.785006, 122.497559),
                    new Rectangle(22.284000, 101.865200, 20.098800, 106.665000),
                    new Rectangle(21.542200, 106.452500, 20.487800, 108.051000),
                    new Rectangle(55.817500, 109.032300, 50.325700, 119.127000),
                    new Rectangle(55.817500, 127.456800, 49.557400, 137.022700),
                    new Rectangle(44.892200, 131.266200, 42.569200, 137.022700),
            };

    public static boolean IsInsideChina(double latitude, double longitude)
    {
        for (int i = 0; i < region.length; i++)
        {
            if (InRectangle(region[i], latitude, longitude))
            {
                for (int j = 0; j < exclude.length; j++)
                {
                    if (InRectangle(exclude[j], latitude, longitude))
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

    private static boolean InRectangle(Rectangle rect, double latitude, double longitude)
    {
        return rect.West <= longitude && rect.East >= longitude && rect.North >= latitude && rect.South <= latitude;
    }
}
